import 'package:flutter/material.dart';
import 'package:flutter_demo/ui/pages/status/data/share_data.dart';
import 'package:flutter_demo/ui/pages/status/provider/multi/b2_page_widget.dart';
import 'package:flutter_demo/ui/pages/status/data/counter_notifier.dart';
import 'package:flutter_demo/ui/pages/status/provider/multi/a2_page_widget.dart';
import 'package:provider/provider.dart';

//参考文档 https://blog.csdn.net/Lastdoggod/article/details/125955791
//参考文档 https://blog.csdn.net/Lastdoggod/article/details/125963144
class MultiProviderTestWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _MultiProviderTestState();
}

class _MultiProviderTestState extends State<MultiProviderTestWidget> {
  @override
  Widget build(BuildContext context) {
    /**
     * 在实际开发过程中肯定会有多个提供者，我们虽然可以采用嵌套的方式来解决，
     * 可读性差，这个时候就需要用到我们的MultiProvider。
     */
    return MultiProvider(
      ///配置共享数据
      providers: [
        ///初始化CounterNotifier
        ChangeNotifierProvider(create: (ctx) => CounterNotifier()),

        StreamProvider(
            create: (context) {
              return Stream<ShareData>.periodic(Duration(milliseconds: 1000),
                      (value) => ShareData(nickname: "tc ...$value", level: 0))
                  .take(10);
            },
            initialData: ShareData(nickname: "正在初始化 ...", level: 0)),
      ],
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [A2PageWidget(), B2PageWidget()],
      ),
    );
  }
}
